﻿Imports Tool
Public Class dlgUpdatePassword
    Private mdtRowUser As DataRow
    Private mstrUserID As String

    Public Sub ShowOpen(ByVal userID As String)
        mstrUserID = userID
        If Not InitDB() Then Exit Sub
        Me.ShowDialog()
    End Sub

    Private Function InitDB() As Boolean
        Dim sql As String
        Dim dt As DataTable
        sql = "Select * From B_User Where UserID ='" & mstrUserID & "'"
        dt = gcnnDBMain.GetDataTable(sql)
        If dt.Rows.Count <> 1 Then
            MsgBox("刷新当前用户信息失败！", MsgBoxStyle.Exclamation, "系统提示")
            Return False
        End If
        mdtRowUser = dt.Rows(0)
        Return True
    End Function

    Private Sub cmdCancel_Click(sender As System.Object, e As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub
    Private Sub cmdOK_Click(sender As System.Object, e As System.EventArgs) Handles cmdOK.Click
        Dim oldPwd As String
        Dim password As String
        Dim sql As String
        oldPwd = IIf(IsDBNull(mdtRowUser.Item("PassWord")), "", mdtRowUser.Item("PassWord"))
        If Len(oldPwd) > 0 Then
            oldPwd = CDBKeyMana.DataCompress(oldPwd, False)
        End If
        If txtOldPwd.Text <> oldPwd Then
            MsgBox("原口令不正确！", MsgBoxStyle.Information, "系统提示")
            txtOldPwd.Focus()
            Exit Sub
        End If

        If txtNewPwd.Text <> txtConfirmPwd.Text Then
            MsgBox("确认口令与新口令不一致！", MsgBoxStyle.Information, "系统提示")
            txtConfirmPwd.Focus()
            Exit Sub
        End If

        Try
            password = txtNewPwd.Text
            If Len(password) > 0 Then
                password = CDBKeyMana.DataCompress(password, True)
            End If

            sql = "Update B_User Set Password='" & password & "'" _
                & " Where UserID='" & mstrUserID & "'"
            gcnnDBMain.ExecuteSql(sql)
            MsgBox("口令修改成功。", MsgBoxStyle.Information, "系统提示")
            Me.Close()
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub
End Class